// components/couponsSelector/couponSelector.js
import * as API_Mall_Coupons from '../../api/mall/coupons'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showList:{
      type:Boolean,
      value:false
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    couponsList:[],
    mineList:[],
    e:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    receive(e){
      if(e.currentTarget.dataset.showText == '已领取'){
        return
      }
      API_Mall_Coupons.receive(e.currentTarget.dataset.id).then(res=>{
            if(res.msg!='success'){
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            }else{
              wx.showToast({
                title: '领取成功',
              })
              this.getList(this.data.e)
            }
      })
    },
    getList(e){
      this.setData({e:e})
      API_Mall_Coupons.getCanReceive(e).then(res=>{
        this.setData({
          couponsList:res.data.canReceive,
          mineList:res.data.mine
        })
        this.setData({
          mineList:res.data.mine
        })
        for(let item of this.data.couponsList){
          item.showText = '点击领取'
          for(let mine of this.data.mineList){
            if(item.id == mine.couponId){
              item.showText = '已领取'
            }
          }
        }
        this.setData({
          couponsList:res.data.canReceive
        })
      })
    },
    showList(){
      // if(this.data.couponsList.length==0){
      //   wx.showToast({
      //     title: '暂无优惠券可使用哦',
      //     icon:'none',
      //     duration:1500
      //   })
      //   return
      // }
      let show = !this.properties.showList
      this.setData({
        showList:show
      })
    }
  },
  lifetimes:{
  }
})
